From 366bd81804a6891e98d55cf77f836c1cd3bfd595 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20H=C3=A4rdeman?= Date: Mon, 10 Nov 2025 23:36:27 +0100 Subject: [PATCH] dhcpv4: update ubus DHCPv4 events/methods MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This is based on the assumption that we don't really have any consumers of ubus DHCPv4 events (yet). With that in mind, rename and simplify the event function (yes, we should add a sibling function for DHCPv6 leases later). Also, take the chance to improve some naming, and introduce additional attributes to the ubus event/method (the interface name and the DUID/IAID, if known). Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/306 Signed-off-by: Álvaro Fernández Rojas --- src/dhcpv4.c | 9 +++------ src/odhcpd.h | 10 ++++------ src/ubus.c | 36 +++++++++++++++++++++++------------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 5aa2309..d1811ec 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -558,8 +558,7 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg req_msg, const uint8_t *re if (!lease) return NULL; - ubus_bcast_dhcp_event("dhcp.release", req_mac, lease->ipv4, - lease->hostname, iface->ifname); + ubus_bcast_dhcpv4_event("dhcp.release4", iface->ifname, lease); dhcpv4_free_lease(lease); lease = NULL; break; @@ -1319,8 +1318,7 @@ void dhcpv4_handle_msg(void *src_addr, void *data, size_t len, } if (reply_msg.data == DHCPV4_MSG_ACK && lease) - ubus_bcast_dhcp_event("dhcp.ack", req->chaddr, lease->ipv4, - lease->hostname, iface->ifname); + ubus_bcast_dhcpv4_event("dhcp.lease4", iface->ifname, lease); } /* Handler for DHCPv4 messages */ @@ -1585,8 +1583,7 @@ static void dhcpv4_valid_until_cb(struct uloop_timeout *event) avl_for_each_element_safe(&iface->dhcpv4_leases, lease, iface_avl, tmp) { if (!INFINITE_VALID(lease->valid_until) && lease->valid_until < now) { - ubus_bcast_dhcp_event("dhcp.expire", lease->hwaddr, lease->ipv4, - lease->hostname, iface->ifname); + ubus_bcast_dhcpv4_event("dhcp.expire4", iface->ifname, lease); dhcpv4_free_lease(lease); update_statefile = true; } diff --git a/src/odhcpd.h b/src/odhcpd.h index d986080..5daf510 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -563,9 +563,8 @@ int ubus_init(void); const char* ubus_get_ifname(const char *name); void ubus_apply_network(void); bool ubus_has_prefix(const char *name, const char *ifname); -void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, - const struct in_addr ipv4, const char *name, - const char *interface); +void ubus_bcast_dhcpv4_event(const char *type, const char *iface, + const struct dhcpv4_lease *lease); #else static inline int ubus_init(void) { @@ -578,9 +577,8 @@ static inline void ubus_apply_network(void) } static inline -void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, - const struct in_addr ipv4, const char *name, - const char *interface) +void ubus_bcast_dhcpv4_event(const char *type, const char *iface, + const struct dhcpv4_lease *lease) { return; } diff --git a/src/ubus.c b/src/ubus.c index ca15d37..bc8e456 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -43,11 +43,18 @@ static int handle_dhcpv4_leases(struct ubus_context *ctx, _unused struct ubus_ob continue; void *m, *l = blobmsg_open_table(&b, NULL); - char *buf = blobmsg_alloc_string_buffer(&b, "mac", 13); + char *buf = blobmsg_alloc_string_buffer(&b, "mac", sizeof(c->hwaddr) * 2 + 1); odhcpd_hexlify(buf, c->hwaddr, sizeof(c->hwaddr)); blobmsg_add_string_buffer(&b); + if (c->duid_len > 0) { + buf = blobmsg_alloc_string_buffer(&b, "duid", DUID_HEXSTRLEN + 1); + odhcpd_hexlify(buf, c->duid, c->duid_len); + blobmsg_add_string_buffer(&b); + blobmsg_add_u32(&b, "iaid", ntohl(c->iaid)); + } + blobmsg_add_string(&b, "hostname", (c->hostname) ? c->hostname : ""); blobmsg_add_u8(&b, "accept-reconf", c->accept_fr_nonce); @@ -130,7 +137,7 @@ static int handle_dhcpv6_leases(_unused struct ubus_context *ctx, _unused struct continue; void *m, *l = blobmsg_open_table(&b, NULL); - char *buf = blobmsg_alloc_string_buffer(&b, "duid", DUID_HEXSTRLEN); + char *buf = blobmsg_alloc_string_buffer(&b, "duid", DUID_HEXSTRLEN + 1); odhcpd_hexlify(buf, a->duid, a->duid_len); blobmsg_add_string_buffer(&b); @@ -392,23 +399,26 @@ static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = { [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING }, }; -void ubus_bcast_dhcp_event(const char *type, const uint8_t *mac, - const struct in_addr ipv4, const char *name, - const char *interface) +void ubus_bcast_dhcpv4_event(const char *type, const char *iface, + const struct dhcpv4_lease *lease) { char ipv4_str[INET_ADDRSTRLEN]; - if (!ubus || !main_object.has_subscribers) + if (!ubus || !main_object.has_subscribers || !iface) return; blob_buf_init(&b, 0); - if (mac) - blobmsg_add_string(&b, "mac", odhcpd_print_mac(mac, ETH_ALEN)); - blobmsg_add_string(&b, "ip", inet_ntop(AF_INET, &ipv4, ipv4_str, sizeof(ipv4_str))); - if (name) - blobmsg_add_string(&b, "name", name); - if (interface) - blobmsg_add_string(&b, "interface", interface); + blobmsg_add_string(&b, "interface", iface); + blobmsg_add_string(&b, "ipv4", inet_ntop(AF_INET, &lease->ipv4, ipv4_str, sizeof(ipv4_str))); + blobmsg_add_string(&b, "mac", odhcpd_print_mac(lease->hwaddr, sizeof(lease->hwaddr))); + if (lease->hostname) + blobmsg_add_string(&b, "hostname", lease->hostname); + if (lease->duid_len > 0) { + char *buf = blobmsg_alloc_string_buffer(&b, "duid", DUID_HEXSTRLEN + 1); + odhcpd_hexlify(buf, lease->duid, lease->duid_len); + blobmsg_add_string_buffer(&b); + blobmsg_add_u32(&b, "iaid", lease->iaid); + } ubus_notify(ubus, &main_object, type, b.head, -1); } -- 2.30.2